package com.ivi.car.base;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright
 * author chengxp
 * date 2018/3/7
 * describe
 * history
 */

public abstract class BaseFragment  extends Fragment implements IBaseView{
    private FragmentActivity mActivity;
    //生命周期绑定 Rxjava中 compose（mLifecycleProvider.bindToLifecycle()）
//    protected final LifecycleProvider<Lifecycle.Event> mLifecycleProvider
//            = AndroidLifecycle.createLifecycleProvider(this);
    protected List<BasePresenter> mPresenterList;

    @Override
    public void onStart() {
        super.onStart();
        mActivity = getActivity();
    }

    public void showLongToast(CharSequence toast) {
        if (!TextUtils.isEmpty(toast)) {
            Toast.makeText(mActivity, toast, Toast.LENGTH_SHORT).show();
        }
    }

    public void startActivity(Class<Activity> clazz) {
        Intent intent = new Intent(mActivity, clazz);
        startActivity(intent);
    }

    @Override
    public void setPresenter(BasePresenter<? extends IBaseView> basePresenter) {
        if (mPresenterList == null) {
            mPresenterList = new ArrayList<>();
        }
        mPresenterList.add(basePresenter);
    }

    @Override
    public void onDestroy() {
        if (mPresenterList != null && mPresenterList.size() > 0) {
            for (BasePresenter basePresenter : mPresenterList) {
                basePresenter.onDestroy();
            }
        }
        super.onDestroy();
    }
}
